/* GUI CONVERTED from reoptra.p (converter v1.75) Mon Nov 13 06:36:24 2000 */
/* reoptra.p - REPETITIVE LABOR TRANSACTION WORK ORDER ROUTING          */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                   */
/* REVISION: 5.0      LAST MODIFIED: 05/26/89   BY: emb       */
/* REVISION: 7.3      LAST MODIFIED: 01/11/93   BY: emb *G689**/
/* REVISION: 7.3      LAST MODIFIED: 02/25/93   BY: emb *G760**/
/* REVISION: 7.3      LAST MODIFIED: 07/14/93   BY: qzl *GD44**/
/* REVISION: 7.3      LAST MODIFIED: 12/07/93   BY: pxd *GG41**/
/* REVISION: 7.3      LAST MODIFIED: 03/14/94   BY: WUG *GJ08**/
/* REVISION: 7.3      LAST MODIFIED: 04/08/94   BY: PXD *FN33**/
/* REVISION: 7.3      LAST MODIFIED: 04/14/94   BY: PXD *FN38**/
/* REVISION: 7.3      LAST MODIFIED: 07/20/94   BY: PXD *GK84**/
/* REVISION: 7.3      LAST MODIFIED: 07/22/94   BY: AIS *GK89**/
/* Oracle changes (share-locks)    09/12/94           BY: rwl *FR25*      */
/* REVISION: 7.3      LAST MODIFIED: 10/22/94   BY: pxd *GN50**/
/* REVISION: 7.3      LAST MODIFIED: 02/15/95   BY: ais *G0FC**/
/* REVISION: 7.3      LAST MODIFIED: 02/28/95   BY: qzl *F0H9*/
/* REVISION: 7.4      LAST MODIFIED: 01/02/97   BY: *G2JV* Julie Milligan */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane      */
/* REVISION: 8.6E     LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan     */
/* REVISION: 8.6E     LAST MODIFIED: 07/17/98   BY: *H1LQ* Niranjan R.    */
/* REVISION: 9.1      LAST MODIFIED: 03/15/99   BY: *N00J* Russ Witt      */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* Mark Brown     */
/* REVISION: 9.1      LAST MODIFIED: 11/08/00   BY: *N0TN* Jean Miller    */

         {mfdeclre.i}
/*N0TN*/ {gplabel.i}

/* ********** Begin Translatable Strings Definitions ********* */

/*N0TN* &SCOPED-DEFINE reoptra_p_1 "Scrap Requirement" */
/* MaxLen: Comment: */

/*N0TN* &SCOPED-DEFINE reoptra_p_2 "Repetitive Schedule" */
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

         define shared variable ophist_recid as recid.
         define variable wr_recid as recid.
         define variable qty_left like op_qty_comp.
         define variable i as integer.
         define shared variable schedule_id like wo_lot.
         define variable temp_qty like op_qty_comp no-undo.
         define variable temp_lot like wo_lot no-undo.
         define variable qty_open like mrp_qty no-undo.
         define variable wod_recid as recid no-undo.
         define variable ii as integer no-undo.
         define buffer woddet for wod_det.
         define variable cons_bck like rpc_sch_bck no-undo.
         define variable cons_fwd like rpc_sch_fwd no-undo.
         define variable rpc_start like rpc_wk_start no-undo.
         define variable start_date like wo_due_date no-undo.
         define variable min_date like wo_due_date no-undo.
         define variable last-op like mfc_logical.
         define variable rpsnbr like mrp_nbr.
         define variable rps_recno as recid.
         define variable rps_duedate like rps_due_date.
         define variable qty_rjct like op_qty_rjct no-undo.
         define variable scrap_pct as decimal no-undo.
         define variable wrlot like wr_lot.
/*N00J*  Begin of new code           */
         define variable use_op_yield  as logical no-undo.
         define variable yield_pct like wo_yield_pct no-undo.
         define variable part-or-routing as character no-undo.
         define variable yield-op like ro_op no-undo.

         for first mrpc_ctrl
            fields (mrpc_op_yield)
            no-lock:  end.
/*N00J*  End of new code           */

         find op_hist where recid(op_hist) = ophist_recid no-lock no-error.
         if not available op_hist then leave.

         qty_left = op_qty_comp.
         qty_rjct = op_qty_rjct.

         find first rpc_ctrl no-lock no-error.
         if available rpc_ctrl then
         assign cons_bck = rpc_sch_bck
                cons_fwd = rpc_sch_fwd
               rpc_start = rpc_wk_start.

         find first wr_route no-lock where wr_lot = op_wo_lot
         and wr_op > op_wo_op no-error.
         if available wr_route then last-op = no. else last-op = yes.

           start_date = op_date.  /* SET TO EFFECTIVE DATE */

         if rpc_start = 8 then min_date = max(today,start_date - cons_bck).
         else
         if rpc_start = 0 then min_date = start_date - cons_bck.
         else do:
            if weekday(start_date) >= rpc_start
            then min_date = max(start_date - cons_bck,
               start_date - weekday(start_date) + rpc_start).
            else min_date = max(start_date - cons_bck,
               start_date - weekday(start_date) + rpc_start - 7).
         end.

         temp_lot = schedule_id.

         LOOP1:
         do while qty_left <> 0
         or qty_rjct <> 0:

            find wr_route no-lock where wr_lot = temp_lot
            and wr_op = op_wo_op no-error.

            do while qty_left <> 0
            or qty_rjct <> 0:

               if available wr_route
               and ((qty_left > 0 and wr_qty_ord > wr_qty_comp)
               or  (qty_left < 0 and wr_qty_comp > 0)
               or qty_rjct <> 0)
               then do:

                  if qty_left > 0 then do:
                     temp_qty = max(wr_qty_ord - max(wr_qty_comp,0),0).
                     temp_qty = min(temp_qty,qty_left).
                     qty_left = max(qty_left - temp_qty,0).
                  end.
                  else do:
                     temp_qty = wr_qty_comp.
                     temp_qty = max(- temp_qty, qty_left).
                     qty_left = qty_left - temp_qty.
                  end.

                  /* UPDATE REPETITIVE SCHEDULE WORK ORDER */
                  wr_recid = recid(wr_route).
                  find wr_route where recid(wr_route) = wr_recid.

                  wr_qty_rjct = wr_qty_rjct + qty_rjct.
                  wr_qty_comp = min(wr_qty_comp + temp_qty, wr_qty_ord).

                  if last-op then do:
                     find wo_mstr exclusive-lock where wo_lot = wr_lot no-error.

                     if wo_qty_ord > wo_qty_comp
                     or (wo_qty_ord = wo_qty_comp and temp_qty < 0)
                     then do:

                        find in_mstr exclusive-lock
                        where in_part = wo_part and in_site =
                        wo_site no-error.
                        if available in_mstr then do:
                          if wo_qty_ord >= 0 then
                          in_qty_ord = in_qty_ord - max(wo_qty_ord - wo_qty_comp
                          - wo_qty_rjct,0).
                        else
                          in_qty_ord = in_qty_ord - min(wo_qty_ord - wo_qty_comp
                          - wo_qty_rjct,0).
                        end.

                        wo_qty_comp = min(wo_qty_comp + temp_qty,wo_qty_ord).

                        if available in_mstr then do:
                          if wo_qty_ord >= 0 then
                          in_qty_ord = in_qty_ord + max(wo_qty_ord - wo_qty_comp
                          - wo_qty_rjct,0).
                        else
                          in_qty_ord = in_qty_ord + min(wo_qty_ord - wo_qty_comp
                          - wo_qty_rjct,0).
                        end.

                        qty_open = max(wo_qty_ord
                                 - max(wo_qty_rjct,0)
                                 - max(wo_qty_comp,0),0).

/*N0TN*/                /* Replaced pre-processor with Term */
                        {mfmrw.i "wo_mstr" wo_part wo_nbr wo_lot """"
                         wo_rel_date wo_due_date qty_open "SUPPLYF" " " wo_site}

/*N0TN*/                /* Replaced pre-processor with Term */
                        {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
                         wo_rel_date wo_due_date
                        "qty_open * (1 - wo_yield_pct / 100)"
                         "DEMAND" SCRAP_REQUIREMENT wo_site}

                     end.

                     /* UPDATE REPETITIVE SCHEDULE RECORD */

                     find rps_mstr exclusive-lock where
                          rps_record = integer(wr_lot) no-error.

                     rps_qty_comp = rps_qty_comp + temp_qty.

                  end.

                  temp_qty = temp_qty + qty_rjct.
                  qty_rjct = 0.

                  /* Offset component requirements given
                     completed operation quantity */
                  for each wod_det no-lock where wod_lot = wr_lot
                  and wod_nbr = wr_nbr
                  and wod_op = wr_op:
/*GUI*/ if global-beam-me-up then undo, leave.


                     do for woddet:

                        wod_recid = recid(wod_det).
                        find woddet exclusive-lock where recid(woddet) = wod_recid.

                        find in_mstr exclusive-lock
                        where in_part = wod_part and in_site =
                        wod_site no-error.
                        if available in_mstr then do:
                           if wod_qty_req >= 0 then
                           in_qty_req = in_qty_req -
                           max(wod_qty_req - wod_qty_iss,0).
                        else
                           in_qty_req = in_qty_req -
                           min(wod_qty_req - wod_qty_iss,0).
                        end.

                        if wod_qty_req > 0
                        then
                           wod_qty_iss = min(wod_qty_req,
                           wod_qty_iss + (temp_qty * wod_qty_req / wr_qty_ord)).
                        else
                           wod_qty_iss = max(wod_qty_req,
                           wod_qty_iss + (temp_qty * wod_qty_req / wr_qty_ord)).

                        if available in_mstr then do:
                            if wod_qty_req >= 0 then
                            in_qty_req = in_qty_req +
                            max(wod_qty_req - wod_qty_iss,0).
                        else
                            in_qty_req = in_qty_req +
                            min(wod_qty_req - wod_qty_iss,0).
                        end.

                        {mfmrwdel.i "wod_det" wod_part wod_nbr wod_lot """"}

                        if wod_qty_req >= 0
                        then qty_open = max(wod_qty_req - max(wod_qty_iss,0),0).
                        else qty_open = min(wod_qty_req - min(wod_qty_iss,0),0).

                        {mfmrw.i "wod_det" wod_part wod_nbr wod_lot
                         string(wod_op) ? wod_iss_date
                         qty_open "DEMAND" " " wod_site}
                     end.
                  end.
/*GUI*/ if global-beam-me-up then undo, leave.


                  /* Non-milestone scheduled operations */
                  wrlot = wr_lot.
                  for each wr_route exclusive-lock where wr_lot = wrlot
                  and wr_op < op_wo_op
                  by wr_lot descending by wr_op descending:
/*GUI*/ if global-beam-me-up then undo, leave.


                     if wr_milestone then leave.
                     if wr_status = "C" then next.

                     wr_qty_comp = min(wr_qty_comp + temp_qty,wr_qty_ord).

                    /* Offset component requirements given
                       completed operation quantity */
                     for each wod_det no-lock where wod_lot = wr_lot
                     and wod_nbr = wr_nbr
                     and wod_op = wr_op:
/*GUI*/ if global-beam-me-up then undo, leave.


                       do for woddet:

                          wod_recid = recid(wod_det).
                          find woddet exclusive-lock where recid(woddet) = wod_recid.

                          find in_mstr exclusive-lock
                          where in_part = wod_part and in_site =
                          wod_site no-error.
                          if available in_mstr then do:
                             if wod_qty_req >= 0 then
                             in_qty_req = in_qty_req -
                             max(wod_qty_req - wod_qty_iss,0).
                          else
                             in_qty_req = in_qty_req -
                             min(wod_qty_req - wod_qty_iss,0).
                          end.

                          if wod_qty_req > 0
                          then wod_qty_iss = min(wod_qty_req,
                           wod_qty_iss + (temp_qty * wod_qty_req / wr_qty_ord)).
                          else wod_qty_iss = max(wod_qty_req,
                           wod_qty_iss + (temp_qty * wod_qty_req / wr_qty_ord)).

                           if available in_mstr then do:
                               if wod_qty_req >= 0 then
                               in_qty_req = in_qty_req +
                               max(wod_qty_req - wod_qty_iss,0).
                           else
                               in_qty_req = in_qty_req +
                               min(wod_qty_req - wod_qty_iss,0).
                           end.

                          {mfmrwdel.i "wod_det" wod_part wod_nbr wod_lot """"}

                          if wod_qty_req >= 0
                          then
                             qty_open = max(wod_qty_req - max(wod_qty_iss,0),0).
                          else
                             qty_open = min(wod_qty_req - min(wod_qty_iss,0),0).

                          {mfmrw.i "wod_det" wod_part wod_nbr wod_lot
                           string(wod_op) ? wod_iss_date
                           qty_open "DEMAND" " " wod_site}
                        end.
                     end.
/*GUI*/ if global-beam-me-up then undo, leave.

                  end.
/*GUI*/ if global-beam-me-up then undo, leave.

               end.

               /* Find other repetitive schedule orders to offset */
               if qty_left <> 0
               or qty_rjct <> 0
               then do:

               /* EXCLUDE REPETITIVE SCHEDULE ORDERS WHERE RPS_DUE_DATE < */
               /* MIN_DATE WHICH TAKES INTO CONSIDERATION RPC_START       */
                 ii = min_date - start_date - 1.

                  release wr_route.
                  next-order:
                  repeat:
/*GUI*/ if global-beam-me-up then undo, leave.

                        ii = ii + 1.
                        if ii > cons_fwd then leave LOOP1.
                                       /* So it's not empty 2nd time thru */

                     if qty_left > 0 then do:
                        find rps_mstr exclusive-lock where rps_part = op_part
                        and rps_site = op_site
                        and rps_line = op_line
                        and rps_due_date = start_date + ii
                        and
                        (
                           rps_qty_req > rps_qty_comp
                           or not last-op
                        )
                        no-error.
                     end.
                     else do:
                        find rps_mstr exclusive-lock where rps_part = op_part
                        and rps_site = op_site
                        and rps_line = op_line
                        and rps_due_date = start_date + ii
                        and
                        (
                           rps_qty_comp > 0
                           or not last-op
                        )
                        no-error.
                     end.

                     if not available rps_mstr then next.
                     temp_lot = string(rps_record).

                     find wr_route no-lock where wr_lot = temp_lot
                     and wr_op = op_wo_op no-error.

         if available wr_route and
         ((qty_left > 0 and wr_qty_comp >= wr_qty_ord) or
          (qty_left <  0 and wr_qty_comp <= 0))
         then
           next next-order.

                     if available wr_route
                     then leave next-order.

                     if not available wr_route and last-op then do:
                        find wo_mstr exclusive-lock use-index wo_lot
                        where wo_lot = temp_lot
                        and wo_part = rps_part
                        and wo_site = rps_site
                        and wo_line = rps_line
                        and wo_type = "S"
                        and wo_status <> "C" no-error.
                        if available wo_mstr
                        and wo_qty_ord > wo_qty_comp
                        then do:

                           if qty_left > 0 then do:
                              temp_qty = max(wo_qty_ord - max(wo_qty_comp,0),0).
                              temp_qty = min(temp_qty,qty_left).
                              qty_left = max(qty_left - temp_qty,0).
                           end.
                           else do:
                              temp_qty = wo_qty_comp.
                              temp_qty = max(- temp_qty, qty_left).
                              qty_left = qty_left - temp_qty.
                           end.

                           find in_mstr exclusive-lock
                           where in_part = wo_part and in_site =
                           wo_site no-error.
                           if available in_mstr then do:
                             if wo_qty_ord >= 0 then
                             in_qty_ord = in_qty_ord -
                             max(wo_qty_ord - wo_qty_comp - wo_qty_rjct,0).
                           else
                             in_qty_ord = in_qty_ord -
                             min(wo_qty_ord - wo_qty_comp - wo_qty_rjct,0).
                           end.

                           wo_qty_comp = min(wo_qty_comp + temp_qty,wo_qty_ord).

                           if available in_mstr then do:
                              if wo_qty_ord >= 0 then
                              in_qty_ord = in_qty_ord +
                              max(wo_qty_ord - wo_qty_comp - wo_qty_rjct,0).
                           else
                              in_qty_ord = in_qty_ord +
                              min(wo_qty_ord - wo_qty_comp - wo_qty_rjct,0).
                           end.

                           qty_open = max(wo_qty_ord
                                    - max(wo_qty_rjct,0)
                                    - max(wo_qty_comp,0),0).

                           {mfmrw.i "wo_mstr" wo_part wo_nbr wo_lot """"
                            wo_rel_date wo_due_date qty_open
                           "SUPPLYF" " " wo_site}

/*N0TN*/                   /* Replaced pre-processor with term */
                           {mfmrw.i "wo_scrap" wo_part wo_nbr wo_lot """"
                            wo_rel_date wo_due_date
                           "qty_open * (1 - wo_yield_pct / 100)"
                            "DEMAND" SCRAP_REQUIREMENT wo_site}

                           rps_qty_comp = rps_qty_comp + temp_qty.
                        end.
                        else do:
                           if qty_left > 0 then do:
                              temp_qty = max(rps_qty_req
                              - max(rps_qty_comp,0),0).
                              temp_qty = min(temp_qty,qty_left).
                              qty_left = max(qty_left - temp_qty,0).
                           end.
                           else do:
                              temp_qty = rps_qty_comp.
                              temp_qty = max(- temp_qty, qty_left).
                              qty_left = qty_left - temp_qty.
                           end.

                           rps_qty_comp = rps_qty_comp + temp_qty.

                           {dateconv.i rps_due_date rpsnbr}
                           rpsnbr = rpsnbr + rps_site.

/*N0TN*/                /* Replaced pre-processor with Term */
                           {mfmrw.i "rps_mstr" rps_part rpsnbr
                            string(rps_record) """" rps_rel_date rps_due_date
                           "max(rps_qty_req - max(rps_qty_comp,0),0)"
                           "SUPPLYF" REPETITIVE_SCHEDULE rps_site}

                           scrap_pct = 0.
                           find ptp_det no-lock where ptp_part = rps_part
                           and ptp_site = rps_site no-error.
                           if available ptp_det
/*N00J*                    then scrap_pct = 1 - ptp_yld_Pct / 100.    */
/*N00J*/                   then assign
/*N00J*/                        use_op_yield = ptp_op_yield
/*N00J*/                        scrap_pct = 1 - ptp_yld_Pct / 100.
                           else do:
                              find pt_mstr no-lock where pt_part = rps_part
                              no-error.
                              if available pt_mstr
/*N00J*                       then scrap_pct = 1 - pt_yield_pct / 100.    */
/*N00J*/                      then assign
/*N00J*/                           use_op_yield = pt_op_yield
/*N00J*/                           scrap_pct = 1 - pt_yield_pct / 100.
                           end.

/*N00J*                    Begin new code...  */
/*N00J*                    CHECK FOR OPERATION YIELD USE  */
                           if available mrpc_ctrl and mrpc_op_yield = yes
                           and use_op_yield = yes then do:

                             /* Determine if Routing code or part should be used... */
                             part-or-routing = if rps_routing <> "" then rps_routing
                               else rps_part.

                             /* pass 9's as operation so all operations will be used */
                             yield-op = 999999999.

                             /* calculate yield percentage... */
                             {gprun.i ""gpcmpyld.p""
                                       "(input part-or-routing,
                                         input rps_rel_date,
                                         input yield-op,
                                         output yield_pct)"}
/*GUI*/ if global-beam-me-up then undo, leave.

                             scrap_pct = 1 - yield_pct / 100.
                           end.
/*N00J*                    End new code...  */

/*N0TN*/                /* Replaced pre-processor with Term */
                           {mfmrw.i "rps_scrap" rps_part rpsnbr
                            string(rps_record) """" rps_rel_date rps_due_date
                           "qty_open * scrap_pct"
                            "DEMAND" SCRAP_REQUIREMENT rps_site}

                        end.
                     end.
                  end.
/*GUI*/ if global-beam-me-up then undo, leave.

               end.
               if not available wr_route then leave.
            end.
            if not available wr_route then leave.
         end.
